#!/usr/bin/env python

import sys
sys.path.insert(0,'/home/mike/projects/non-mike/ReportLab/trunk/reportlab/build/lib.linux-i686-2.2/')

from reportlab.pdfgen import canvas
from reportlab.lib import pagesizes
from reportlab.lib.units import inch, cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.styles import ParagraphStyle

from reportlab.platypus import Frame
from reportlab.platypus import Paragraph
from reportlab.platypus.flowables import Spacer
from reportlab.platypus.doctemplate import PageTemplate
from reportlab.platypus.doctemplate import BaseDocTemplate

(width,height) = pagesizes.landscape(pagesizes.LETTER)

class TwoColumnLandscapePageTemplate(PageTemplate):
    def __init__(self,*args,**kwargs):
        PageTemplate.__init__(self,*args,**kwargs)

        self.pagesize = pagesizes.landscape(pagesizes.LETTER)
        self.frames = [
            Frame(cm,2*cm,width/2.0-0.5*cm,height-3.5*cm),
            Frame(cm+width/2.0,2*cm,width/2.0-2*cm,height-3.5*cm)
            ]
        self.onPage = self._onPage
        self.onPageEnd = self._onPageEnd


    def _onPage(self, canvas, doc):
        return None

    def _onPageEnd(self, canvas, doc):
        canvas.setFont('Times-Italic',32)
        canvas.drawString(cm,height-cm,'Activist Network Event Listings')
        canvas.setFont('Helvetica',16)
        foo = 'http://calgary.activist.ca'
        canvas.drawString(width-cm-canvas.stringWidth(foo,'Helvetica',16),height-cm,foo)
        canvas.line(0.1*cm,height-1.25*cm,width-0.1*cm,height-1.25*cm)

        canvas.line(0.1*cm,2*cm,width-0.2*cm,2*cm)

        style = ParagraphStyle(name='Normal',
                               fontName='Helvetica',
                               fontSize=8,
                               leading=9)
        explanation = [
            Paragraph('''<b>The Activist Network</b> is a social justice organisation concerned with informing others of events, meetings, protests, pot-lucks, parties, conferences and the like. The Network is <b>run by volunteers</b>. If you are interested in helping (we always can use progammers, moderators, and out-reach volunteers or anything else you can think of), please use the Web site to send a note to others already involved. You may also use the Web site to search the events for the next meeting...''',
                      style),
            ]
        
        mywidth = width - 0.5*cm
        myspacing = 0.25*cm
        x = 0.1*cm
        bottom = Frame(x,0.1*cm,mywidth,1.8*cm)
        canvas.setFillGray(0.5)
        while len(explanation):
            front = explanation[0]
            explanation = explanation[1:]
            if bottom.add(front, canvas):
                continue

            more = front.split(bottom._getAvailableWidth(), bottom._y)
            if len(more):
                while len(more) and bottom.add(more[0],canvas):
                    more = more[1:]
                if len(more):
                    explanation = more + explanation
                
            x = x + mywidth + (0.25*cm)
            bottom = Frame(x,0.1*cm,mywidth,1.8*cm)
            

class ActivistSimpleDocTemplate(BaseDocTemplate):
    pass


doc = ActivistSimpleDocTemplate( 'activist-test.pdf',
                                 pageTemplates=[TwoColumnLandscapePageTemplate(id='two-column')],
                                 showBounary=True,
                                 leftMargin=cm,
                                 rightMargin=cm,
                                 topMargin=cm,
                                 bottomMargin=cm,
                                 title='Activist Network Event Listings',
                                 author='http://activist.ca' )


events = []
for x in range(20):
    events.append( Paragraph('''<font face=Helvetica size=12>Some Event</font> (<font face=Helvetica-Oblique size=11>January 23, 2005 @ 18:00</font>) -- This is the event description. If a lazy fox were here he might jump over some brown cows. This is the event description. If a lazy fox were here he might jump over some brown cows. This is the event description. If a lazy fox were here he might jump over some brown cows. <font face="Courier">#123 Fake Street, Calgary, Alberta, Canada.</font> <i>First Lastname (403) 123-4567</i>''',
                             ParagraphStyle(name='Normal',
                                            fontName='Times-Roman',
                                            fontSize=10,
                                            leading=12)
                             ) )
    events.append( Spacer(0,0.2*cm) )


   
from StringIO import StringIO
file = StringIO()
doc.build(events, filename=file)
open('activist-test.pdf','wb').write(file.getvalue())
sys.exit(0)



(width,height) = pagesizes.landscape(pagesizes.LETTER)
left_margin = cm

def drawPageFrame(canv):
    #print canv.getAvailableFonts()
    canv.setFont('Times-Italic',32)
    canv.drawString(cm,height-cm,'Activist Network Event Listings')
    canv.setFont('Helvetica',16)
    foo = 'http://calgary.activist.ca'
    canv.drawString(width-cm-canv.stringWidth(foo,'Helvetica',16),height-cm,foo)
    canv.line(0.1*cm,height-1.25*cm,width-0.1*cm,height-1.25*cm)

    fr0 = Frame(cm,height-2*cm,width/2.0,height-2.5*cm)
    fr1 = Frame(cm+width/2.0,height-2*cm,width/2.0,height-2.5*cm)
    print dir(fr0)
    canv.setFont("Times-Bold", 12)
    event = Paragraph("this is some text. "*600, getSampleStyleSheet()['Normal'])
    fr0.add(event,canv)
    print dir(event)
    print event.splitOn.__doc__
    print event.drawPara.__doc__
    canv.translate(cm,height-2.5*cm-event.height)
    event.canv = canv
    print event.drawPara()
    #canv.drawText(tx)

    canv.showPage()

canv = canvas.Canvas('activist-test.pdf', invariant=1, pagesize=pagesizes.landscape(pagesizes.LETTER))
canv.setPageCompression(1)
drawPageFrame(canv)
#canv.save()
open('activist-test.pdf','wb').write(canv.getpdfdata())


